package evan.zhang.flags;

//import java.util.ArrayList;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class DistBoundaryOverlay extends Overlay {

	private static final String tag = "FlagsOnMap::DistBoundaryOverlay";
	private ArrayList<GeoPoint> pointsList = new ArrayList<GeoPoint>(); //leave this as a dynamic structure
	private ArrayList<String> pointsStringList = new ArrayList<String>();//leave this as a dynamic structure
	//int numOfPolygon = 0;
	//int[] numOfPointsPerPolygon;
	
	public DistBoundaryOverlay() {
		super();
	}
	
	public DistBoundaryOverlay(String dist, Context context) {
		// TODO Auto-generated constructor stub
	//	super();
		//read the related dist boundary point file and get the following numbers
		
		    
		        
		        XmlResourceParser xrp = context.getResources().getXml(R.xml.china_boundary); 
		        
		       // XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		       //  factory.setNamespaceAware(true);
		       //  XmlPullParser xpp = factory.newPullParser();
		        try {
		        // xrp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
		         int eventType = xrp.getEventType();
		         while (eventType != XmlPullParser.END_DOCUMENT) {
		          if(eventType == XmlPullParser.START_DOCUMENT) {
		              //System.out.println("Start document");
		          } else if(eventType == XmlPullParser.END_DOCUMENT) {
		             // System.out.println("End document");
		          } else if(eventType == XmlPullParser.START_TAG) {
		        	  if (xrp.getName().equals("coordinates")) {
		        		  xrp.next();
		        		 //Log.e(tag,xrp.getText());
		        	     pointsStringList.add(xrp.getText());
		        	  }
		        		  
		              //System.out.println("Start tag "+xrp.getName());
		          } else if(eventType == XmlPullParser.END_TAG) {
		              //System.out.println("End tag "+xrp.getName());
		          } else if(eventType == XmlPullParser.TEXT) {
		              //System.out.println("Text "+xrp.getText());
		          }
		          eventType = xrp.next();
		         }
		        }catch (XmlPullParserException e) {
		        	Log.e(tag,e.toString());
		        }catch (IOException e) {
		        	Log.e(tag,e.toString());
		        }
		
			//pointsStringList.add("109.5667037963867,18.16888046264648,0 109.5635986328125,18.25165939331055,0 109.4738998413086,18.20804977416992,0 109.4941024780274,18.24138069152832,0 109.4471969604492,18.2822208404541,0 109.2227020263672,18.30304908752441,0 109.1703033447266,18.28083038330078,0 109.1350021362305,18.35471916198731,0 108.8818969726563,18.42694091796875,0 108.7829971313477,18.48637962341309,0 108.6893997192383,18.50082969665527,0 108.6710968017578,18.56332969665527,0 108.6947021484375,18.69444084167481,0 108.6678009033203,18.79166030883789,0 108.622200012207,18.83333015441895,0 108.6540985107422,18.92943954467773,0 108.6168975830078,19.09666061401367,0 108.6829986572266,19.14944076538086,0 108.6279983520508,19.26499938964844,0 108.690299987793,19.38109970092773,0 108.7529983520508,19.3830509185791,0 108.9563980102539,19.53443908691406,0 109.2041015625,19.69277000427246,0 109.2300033569336,19.7269401550293,0 109.3050003051758,19.70972061157227,0 109.303596496582,19.78277015686035,0 109.2118988037109,19.75165939331055,0 109.1660995483399,19.72277069091797,0 109.1622009277344,19.79748916625977,0 109.2865982055664,19.91694068908691,0 109.5143966674805,19.85333061218262,0 109.5530014038086,19.95527076721191,0 109.5801010131836,19.98307037353516,0 109.7065963745117,20.0072193145752,0 109.8000030517578,19.95026969909668,0 109.817497253418,19.98805046081543,0 109.8386001586914,19.9263801574707,0 109.9349975585938,19.99221992492676,0 109.9785995483398,19.9447193145752,0 109.9894027709961,19.90777015686035,0 110.0410995483398,19.97748947143555,0 110.1177978515625,19.99888038635254,0 110.1524963378906,20.06055068969727,0 110.3419036865234,20.05526924133301,0 110.381103515625,20.03499984741211,0 110.3805007934571,20.07471084594727,0 110.578598022461,19.97138023376465,0 110.6072006225586,19.9205493927002,0 110.6080017089844,19.98944091796875,0 110.5682983398438,20.03554916381836,0 110.5947036743164,20.10666084289551,0 110.6776962280274,20.15777015686035,0 110.7690963745117,20.01693916320801,0 110.9000015258789,19.99888038635254,0 110.9308013916016,20.00138092041016,0 110.995002746582,19.74276924133301,0 111.0310974121094,19.63999938964844,0 110.9665985107422,19.63332939147949,0 110.8683013916016,19.53610992431641,0 110.8265991210938,19.54972076416016,0 110.8177032470703,19.59638023376465,0 110.75830078125,19.45083045959473,0 110.625,19.2461109161377,0 110.5836029052734,19.15637969970703,0 110.4804992675781,19.16777038574219,0 110.5102005004883,19.1261100769043,0 110.5475006103516,19.13833045959473,0 110.5658035278321,19.06554985046387,0 110.5115966796875,18.97999954223633,0 110.4602966308594,18.85220909118652,0 110.4319000244141,18.82666015625,0 110.4888992309571,18.7927703857422,0 110.4969024658203,18.87554931640625,0 110.5341033935547,18.78443908691406,0 110.432502746582,18.72916030883789,0 110.4139022827148,18.66472053527832,0 110.3964004516602,18.70360946655273,0 110.1416015625,18.56415939331055,0 110.0352020263672,18.50609970092773,0 110.0804977416992,18.4888801574707,0 110.0511016845703,18.38528060913086,0 109.9700012207031,18.38166046142578,0 109.9914016723633,18.43083000183106,0 109.8293991088867,18.38109970092773,0 109.752799987793,18.40221977233887,0 109.7268981933594,18.25498962402344,0 109.7065963745117,18.19887924194336,0 109.6757965087891,18.23833084106445,0 109.6027984619141,18.2077808380127,0 109.5667037963867,18.16888046264648,0 ");
	}
	
	public void getPointsList(String pointsString) {
		 String[] temp = pointsString.split(",0 ");
		 
		 Calendar cal = Calendar.getInstance();
		// Log.e("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz","start time is = "+cal.getTime().toString());
		 for (int i=0; i<temp.length; i++) {
			// Log.e("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",temp[i]+"\n");
			 String[] pair = temp[i].split(","); 
			 pointsList.add(new GeoPoint((int)(Double.parseDouble(pair[1])*1E6),(int)(Double.parseDouble(pair[0])*1E6)));
			 
			// Log.e("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",pointsList.get(i).toString());
		 }
		 
		Log.e("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz","end time is = "+cal.getTime().toString());
	}
	
	public void drawPolygon(Canvas canvas, MapView mapView, String pointsString) {
		
		//Log.e(tag,"how many polygon?");
		getPointsList(pointsString);
		//mapView here is used to get projection
		Projection projection = mapView.getProjection();
		 
		Paint paint = new Paint(); 
		paint.setARGB(250, 255, 0, 0); 
		//paint.setARGB(100, 100, 100, 100);
		//paint.setAntiAlias(true); 
		//paint.setFakeBoldText(true);
		
		Point fromto = new Point(); 
		GeoPoint geofromto = pointsList.get(0);
		projection.toPixels(geofromto, fromto);
		Path path = new Path();
		//path.setLastPoint(fromto.x, fromto.y);
		path.moveTo(fromto.x, fromto.y);
		
		for (Iterator<GeoPoint> it = pointsList.iterator(); it.hasNext(); ) {
		    geofromto = it.next();  // No downcasting required.
		    projection.toPixels(geofromto, fromto);
		    path.lineTo(fromto.x, fromto.y);
		}
		
		//PathShape pathShape = new PathShape(path,10,10);
		//ShapeDrawable shapeDrawable = new ShapeDrawable(pathShape);
		
		//Path path = new Path();
		//int rad=5;
		//RectF oval = new RectF(myPoint.x-rad, myPoint.y-rad, myPoint.x+rad, myPoint.y+rad);
		//canvas.drawOval(oval, paint);
		//canvas.drawLine(myPoint.x, myPoint.y, myPoint1.x, myPoint1.y, paint);
		
		canvas.drawPath(path, paint);
		//pathShape.draw(canvas,paint);

	}
	
   
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		Log.e(tag,"how many draw?");
		for (Iterator<String> it = pointsStringList.iterator(); it.hasNext(); ) {
			drawPolygon(canvas, mapView, it.next());
		}
		
	}
}